package com.sntk.organauthomanager.infrastructure.dao;

import com.sntk.organauthomanager.domain.model.Resource;
import com.sntk.organauthomanager.domain.repository.ResourceRepository;
import com.sntk.organauthomanager.infrastructure.convert.ResourceConvert;
import com.sntk.organauthomanager.infrastructure.db.entity.ResourceEntity;
import com.sntk.organauthomanager.infrastructure.db.repository.ResourceDBRepository;
import com.sntk.organauthomanager.infrastructure.exception.ObjectExistedException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * @ClassName: ResourceRepositoryImpl
 * @author: wgchao
 * @createTime: 2020/7/21 4:40 PM
 * @Description: TODO
 */
@Component
public class ResourceRepositoryImpl implements ResourceRepository {
    @Autowired
    private ResourceDBRepository resourceDBRepository;
    @Autowired
    private ResourceConvert resourceConvert;

    @Override
    public void add(Resource resource, String operator) {
        ResourceEntity resourceEntity = resourceDBRepository.findFirstByMarkAndDelFalse(resource.getMark());
        if(resource.isAdd()){
            if(resourceEntity != null){
                throw new ObjectExistedException("资源标识已被使用");
            }
            resourceEntity = resourceConvert.convert(resource);
            resourceEntity.setDel(false);
            resourceEntity.setCreator(operator);
            resourceEntity.setCreateTime(new Date());
            resourceEntity.setLastUpdateTime(new Date());
            resourceEntity.setLastUpdator(operator);
            resourceEntity = resourceDBRepository.save(resourceEntity);
        }else{
            if(resourceEntity == null){
                throw new ObjectExistedException("资源不存在");
            }
            ResourceEntity resourceEntity1 = resourceConvert.convert(resource);
            resourceEntity1.setDel(resourceEntity.getDel());
            resourceEntity1.setCreator(resourceEntity.getCreator());
            resourceEntity1.setCreateTime(resourceEntity.getCreateTime());
            resourceEntity1.setLastUpdateTime(new Date());
            resourceEntity1.setLastUpdator(operator);
            resourceEntity1 = resourceDBRepository.save(resourceEntity1);
        }
    }

    @Override
    public Resource findByKey(String s) {
        ResourceEntity resourceEntity = resourceDBRepository.findFirstByMarkAndDelFalse(s);
        if(resourceEntity == null){
            return null;
        }
        return resourceConvert.convert(resourceEntity);
    }

    @Override
    public void delete(String s, String operator) {
        ResourceEntity resourceEntity = resourceDBRepository.findFirstByMarkAndDelFalse(s);
        if(resourceEntity == null){
            throw new ObjectExistedException("资源不存在");
        }
        resourceEntity.setLastUpdator(operator);
        resourceEntity.setLastUpdateTime(new Date());
        resourceEntity.setDel(true);
        resourceEntity = resourceDBRepository.save(resourceEntity);
    }
}
